//
//  XMMyBrowsHistoryViewController.m
//  XMJJ_zml
//
//  Created by 丁浩 on 2025/5/28.
//

#import "XMMyBrowsHistoryViewController.h"
#import "XMBrowsHistoryView.h"
#import "XMNetwork+mine.h"
#import "XMMyBrowseProductModel.h"
#import "XMMeetDetailViewController.h"

@interface XMMyBrowsHistoryViewController ()<XMBrowsHistoryViewDelegate>

@property (nonatomic, strong) XMBrowsHistoryView *browsHistoryView;

@property (nonatomic, assign) NSInteger page;
@property (nonatomic, assign) NSInteger tapSelectIndex;

@end

@implementation XMMyBrowsHistoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self creatUI];
}

- (void)creatUI {
    [self.view addSubview:self.browsHistoryView];
    [self getMyBrowseMeet];
}

//作品浏览记录
- (void)getMyBrowsHisVideo {
    XMWeakSelf;
    [_Network getMyBrowsHistoryWithPage:[NSString stringWithFormat:@"%ld", _page]
                                success:^(id  _Nonnull responseObject) {
        NSArray *browseModelArr = [NSArray yy_modelArrayWithClass:[XMMyBrowseProductModel class] json:[responseObject[@"data"] safeArrayForKey:@"records"]];
        [weakSelf.browsHistoryView setContentWithHistoryVideoModelArr:browseModelArr
                                                                 page:weakSelf.page];
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

//见面浏览记录
- (void)getMyBrowseMeet {
    XMWeakSelf;
    [_Network getMyBrowseMeetHisWithPage:[NSString stringWithFormat:@"%ld", _page]
                                 success:^(id  _Nonnull responseObject) {
        NSArray *browseModelArr = [NSArray yy_modelArrayWithClass:[XMMyBrowseProductModel class] json:[responseObject[@"data"] safeArrayForKey:@"records"]];
        [weakSelf.browsHistoryView setContentWithHistoryVideoModelArr:browseModelArr
                                                                 page:weakSelf.page];
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

//清空见面浏览记录
- (void)meetBrowseClear {
    XMWeakSelf;
    [_Network clearMyBrowseMeetSuccess:^(id  _Nonnull responseObject) {
        [weakSelf refreshData];
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

//清空视频浏览记录
- (void)videoBrowseClear {
    XMWeakSelf;
    [_Network deleteAllBrowseSuccess:^(id  _Nonnull responseObject) {
        [weakSelf refreshData];
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

- (void)meetCheckDetailWithServiceId:(NSString *)serviceId {
    XMMeetDetailViewController *meetDetailVC = [[XMMeetDetailViewController alloc] init];
    meetDetailVC.serviceId = serviceId;
    [self.navigationController pushViewController:meetDetailVC animated:YES];
}

- (void)tapSelect:(NSInteger)indexSelect {
    _page = 1;
    _tapSelectIndex = indexSelect;
    if (indexSelect == 0) {
        [self getMyBrowseMeet];
    }
    else {
        [self getMyBrowsHisVideo];
    }
}

- (void)refreshData {
    _page = 1;
    if (_tapSelectIndex == 0) {
        [self getMyBrowseMeet];
    }
    else {
        [self getMyBrowsHisVideo];
    }
}

- (void)loadMore {
    _page += 1;
    if (_tapSelectIndex == 0) {
        [self getMyBrowseMeet];
    }
    else {
        [self getMyBrowsHisVideo];
    }
}

#pragma mark - Init
- (XMBrowsHistoryView *)browsHistoryView {
    if (!_browsHistoryView) {
        _browsHistoryView = [[XMBrowsHistoryView alloc] initWithFrame:self.view.bounds];
        _browsHistoryView.delegate = self;
    }
    return _browsHistoryView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
